﻿

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Resources;
using System.Reflection;
//using DAL.DBFactory;

namespace CFSystem.JXC
{
    public partial class FrmGoodInfo : Form
    {
        public FrmGoodInfo()
        {
            InitializeComponent();
            this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer, true);
            this.UpdateStyles();
            this.LoadImage();
        }

        /// <summary>
        /// 初始化窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmGoodInfo_Load(object sender, EventArgs e)
        {
            this.LoadTreeView();
            this.TestDb();
            this.DataBind(); //绑定单元格

            //Bitmap bt = new Bitmap("背景1.png");
            //this.BackgroundImage = bt;
        }

        private void TestDb()
        {
            
            string connString = @"data source=.\sqlexpress;initial catalog=JXC;persist security info=False;user id=test;password=123";
            string commString = "select * from tbl_Vip";
           
            ExecuteDB.ConnectionString = connString;
            ExecuteDB.DBType = "sql";
            dataGridView2.DataSource = ExecuteDB.ExcuteSql(commString).Tables[0];
         
        }

        private void LoadImage()
        {
            #region "obsolete"
            //Assembly myAssem = Assembly.GetEntryAssembly();  
            //ResourceManager rm = new ResourceManager("PFAddin1.Background",Assembly.GetExecutingAssembly());

            ////TheAresAdoNet   是你程序的命名空间,TheAres是TheAres.resources  
            //this.BackgroundImage = (Image) rm.GetObject("MyLogo");

            //this.Text = rm.GetString("Title");
            #endregion
            button1.Image = imageList1.Images[2];
            button2.Image = imageList1.Images[4];
            button3.Image = imageList1.Images[6];
            button4.Image = imageList1.Images[8];
            button5.Image = imageList1.Images[0];
        }

        /// <summary>
        /// 加载树结构
        /// </summary>
        private void LoadTreeView()
        {
            TreeNode tn = new TreeNode();
            tn.NodeFont = new Font("Tahoma", 9);
            tn.ForeColor = Color.Blue;
            tn.ImageIndex = 0;
            tn.SelectedImageIndex = 1;
            tn.Text = "所有分类";

            TreeNode tnChild = new TreeNode();
            tnChild.NodeFont = new Font("Tahoma", 9);
            tnChild.ForeColor = Color.Blue;
            tnChild.ImageIndex = 0;
            tnChild.SelectedImageIndex = 1;
            tnChild.Text = "上衣";

            TreeNode tnChild1 = new TreeNode();
            tnChild1.NodeFont = new Font("Tahoma", 9);
            tnChild1.ForeColor = Color.Blue;
            tnChild1.ImageIndex = 0;
            tnChild1.SelectedImageIndex = 1;
            tnChild1.Text = "裤子";

            TreeNode tnChild2 = new TreeNode();
            tnChild2.NodeFont = new Font("Tahoma", 9);
            tnChild2.ForeColor = Color.Blue;
            tnChild2.ImageIndex = 0;
            tnChild2.SelectedImageIndex = 1;
            tnChild2.Text = "箱包";

            TreeNode tnChild3 = new TreeNode();
            tnChild3.NodeFont = new Font("Tahoma", 9);
            tnChild3.ForeColor = Color.Blue;
            tnChild3.ImageIndex = 0;
            tnChild3.SelectedImageIndex = 1;
            tnChild3.Text = "鞋子";

            TreeNode tnChild4 = new TreeNode();
            tnChild4.NodeFont = new Font("Tahoma", 9);
            tnChild4.ForeColor = Color.Blue;
            tnChild4.ImageIndex = 0;
            tnChild4.SelectedImageIndex = 1;
            tnChild4.Text = "裙子";

            treeView1.Nodes.Add(tn);

            tn.Nodes.Add(tnChild);
            tn.Nodes.Add(tnChild1);
            tn.Nodes.Add(tnChild2);
            tn.Nodes.Add(tnChild3);
            tn.Nodes.Add(tnChild4);

            treeView1.BackColor = Color.WhiteSmoke;
            treeView1.ImageList = imageList2;
            treeView1.ImageIndex = 0;
            treeView1.BorderStyle = BorderStyle.Fixed3D;

            
        }

        /// <summary>
        /// 加载表格数据
        /// </summary>
        private void DataBind()
        {
            DataTable dt = new DataTable();

            DataGridTextBoxColumn datagridcolum = new DataGridTextBoxColumn();

            //Create a new column for datatable.
            DataColumn dc = new DataColumn("商品编号", System.Type.GetType("System.String"));
            //Add created column to datatable object.
            dt.Columns.Add(dc);
            //Create a new column for datatable.
            dc = new DataColumn("名称", System.Type.GetType("System.String"));
            //Add created column to datatable object.
            dt.Columns.Add(dc);
            dc = new DataColumn("款号", System.Type.GetType("System.String"));
            //Add created column to datatable object.
            dt.Columns.Add(dc);

            //1.
            dc = new DataColumn("类别", System.Type.GetType("System.String"));
            //Add created column to datatable object.
            dt.Columns.Add(dc);

            //2.
            dc = new DataColumn("尺码", System.Type.GetType("System.String"));
            //Add created column to datatable object.
            dt.Columns.Add(dc);

            //3.
            dc = new DataColumn("季节", System.Type.GetType("System.String"));
            //Add created column to datatable object.
            dt.Columns.Add(dc);

            //4.
            dc = new DataColumn("颜色", System.Type.GetType("System.String"));
            //Add created column to datatable object.
            dt.Columns.Add(dc);

            DataRow dr;
            for (int i = 1; i <= 5; i++)
            {
                int j = i;
                dr = dt.NewRow();
                dr["商品编号"] = "001" + i.ToString();
                if (j < 3)
                {
                    dr["名称"] = "女士手提包" + i.ToString();
                }
                else
                {
                    dr["名称"] = "经典牛仔裤";
                }
                dr["款号"] = "9192" + i.ToString();
                dr["类别"] = "裤子";
                dr["尺码"] = "XL";
                dr["季节"] = "2009冬";
                dr["颜色"] = "红色";
                dt.Rows.Add(dr);
            }
            //Add datatable object to dataset object.
            dataGridView1.DataSource = dt;
            SetDefaultCellStyles();
        }

        /// <summary>
        /// 该函数实现单元格合并与选中合并单元格时候背景色高亮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            #region "obsolete"
            //if (e.RowIndex == -1 && e.ColumnIndex == 0)
            //{
            //    using (Brush backColorBrush = new SolidBrush(e.CellStyle.BackColor))
            //    {
            //        e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
            //        e.PaintBackground(e.ClipBounds, true);

            //        if (e.Value != null)
            //        {
            //            e.Graphics.DrawString("item A", e.CellStyle.Font,Brushes.Crimson, e.CellBounds.X + 2,
            //                e.CellBounds.Y + 2, StringFormat.GenericDefault);
            //            //Font t = new Font(e.CellStyle.Font.FontFamily, 5.0f);
            //            //e.Graphics.DrawString("1", t, Brushes.Crimson, e.CellBounds.X + 38, e.CellBounds.Y + 9);
            //        }
            //        e.Handled = true;
            //    }

            //}
            #endregion

            #region "选中背景色高亮"
            if ((e.State & DataGridViewElementStates.Selected) == DataGridViewElementStates.Selected)
            {
                return;
            }

            using
                (
                Brush gridBrush = new SolidBrush(this.dataGridView1.GridColor),
                backColorBrush = new SolidBrush(e.CellStyle.BackColor)
                )
            {
                using (Pen gridPenColor = new Pen(Color.Gray))
                {
                    // gridPenColor.Color = Color.Red;

                    Rectangle rect1 = new Rectangle(e.CellBounds.Location, e.CellBounds.Size);
                    Rectangle rect2 = new Rectangle(e.CellBounds.Location, e.CellBounds.Size);

                    rect1.X -= 1;
                    rect1.Y -= 1;

                    rect2.Width -= 1;
                    rect2.Height -= 1;
                    //画矩形
                    e.Graphics.DrawRectangle(gridPenColor, rect1);  // must draw border for grid scrolling horizontally
                    //填充矩形
                    e.Graphics.FillRectangle(backColorBrush, rect2);
                }

                e.PaintContent(e.CellBounds);  // output cell text

                e.Handled = true;
            }
            #endregion

            #region "相同内容单元格合并"
            //if (e.ColumnIndex == 0 && e.RowIndex != -1)
            if (e.ColumnIndex > 0 && e.RowIndex != -1)
            {
                using
                (
                Brush gridBrush = new SolidBrush(this.dataGridView1.GridColor),
                backColorBrush = new SolidBrush(e.CellStyle.BackColor)
                )
                {
                    using (Pen gridLinePen = new Pen(gridBrush))
                    {
                        //gridLinePen.Color = Color.Red;
                        // 
                        e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
                        // Grid 
                        // 
                        if (e.RowIndex < dataGridView1.Rows.Count - 1 &&
                        dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() !=
                        e.Value.ToString())
                            e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left,
                            e.CellBounds.Bottom - 1, e.CellBounds.Right - 1,
                            e.CellBounds.Bottom - 1);

                        // 
                        e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,
                        e.CellBounds.Top, e.CellBounds.Right - 1,
                        e.CellBounds.Bottom);

                        //
                        if (e.Value != null)
                        {
                            if (e.RowIndex > 0 &&
                            dataGridView1.Rows[e.RowIndex - 1].Cells[e.ColumnIndex].Value.ToString() ==
                            e.Value.ToString())
                            {

                            }
                            else
                            {
                                e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
                                Brushes.Black, e.CellBounds.X + 2,
                                e.CellBounds.Y + 5, StringFormat.GenericDefault);
                            }

                        }
                        e.Handled = true;
                    }
                }

            }
            #endregion

        }

        /// <summary>
        /// 设置单元格样式
        /// </summary>
        private void SetDefaultCellStyles()
        {
            this.dataGridView1.DefaultCellStyle.BackColor = Color.Beige;
            this.dataGridView1.DefaultCellStyle.Font = new Font("Tahoma", 12);
            this.dataGridView1.BackgroundColor = Color.White;

            DataGridViewRow row = this.dataGridView1.RowTemplate;
            row.DefaultCellStyle.BackColor = Color.Bisque;
            row.Height = 25;

            row.MinimumHeight = 20;
            DataGridViewCellStyle style = dataGridView1.ColumnHeadersDefaultCellStyle;

            style.BackColor = Color.Green;
            style.ForeColor = Color.White;
            style.Font = new Font(dataGridView1.DefaultCellStyle.Font, FontStyle.Bold);
            style.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dataGridView1.Columns[0].Width = 200;

        }

        private void SetUpDataGridView()
        {
            DataGridViewCellStyle style = dataGridView1.ColumnHeadersDefaultCellStyle;
            style.BackColor = Color.Navy;
            style.ForeColor = Color.White;
            style.Font = new Font(dataGridView1.Font, FontStyle.Bold);

            dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter;
            dataGridView1.Name = "dataGridView1";
            //dataGridView1.Location = new Point(8, 8);
            //dataGridView1.Size = new Size(500, 300);
            dataGridView1.AutoSizeRowsMode =
                DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders;
            dataGridView1.ColumnHeadersBorderStyle =
                DataGridViewHeaderBorderStyle.Raised;
            dataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.Single;
            dataGridView1.GridColor = SystemColors.ActiveBorder;
            dataGridView1.RowHeadersVisible = false;

            dataGridView1.Columns[0].Name = "Release Date";
            dataGridView1.Columns[1].Name = "Track";
            dataGridView1.Columns[1].DefaultCellStyle.Alignment =
                DataGridViewContentAlignment.MiddleCenter;
            dataGridView1.Columns[2].Name = "Title";
            //dataGridView1.Columns[3].Name = "Artist";
            //dataGridView1.Columns[4].Name = "Album";

            // Make the font italic for row four.
            // dataGridView1.Columns[4].DefaultCellStyle.Font = new Font(DataGridView.DefaultFont, FontStyle.Italic);

            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView1.MultiSelect = false;

            dataGridView1.BackgroundColor = Color.Honeydew;

            dataGridView1.Dock = DockStyle.Fill;

        }

        #region "obsolete"
        //// Paints the content that spans multiple columns and the focus rectangle.
        //void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        //{
        //    // Calculate the bounds of the row.
        //    Rectangle rowBounds = new Rectangle(
        //        this.dataGridView1.RowHeadersWidth, e.RowBounds.Top,
        //        this.dataGridView1.Columns.GetColumnsWidth(
        //            DataGridViewElementStates.Visible) -
        //        this.dataGridView1.HorizontalScrollingOffset + 1,
        //        e.RowBounds.Height);

        //    SolidBrush forebrush = null;
        //    try
        //    {
        //        // Determine the foreground color.
        //        if ((e.State & DataGridViewElementStates.Selected) ==
        //            DataGridViewElementStates.Selected)
        //        {
        //            forebrush = new SolidBrush(e.InheritedRowStyle.SelectionForeColor);
        //        }
        //        else
        //        {
        //            forebrush = new SolidBrush(e.InheritedRowStyle.ForeColor);
        //        }

        //        // Get the content that spans multiple columns.
        //        object recipe =
        //            this.dataGridView1.Rows.SharedRow(e.RowIndex).Cells[2].Value;

        //        if (recipe != null)
        //        {
        //            String text = recipe.ToString();

        //            // Calculate the bounds for the content that spans multiple 
        //            // columns, adjusting for the horizontal scrolling position 
        //            // and the current row height, and displaying only whole
        //            // lines of text.
        //            Rectangle textArea = rowBounds;
        //            textArea.X -= this.dataGridView1.HorizontalScrollingOffset;
        //            textArea.Width += this.dataGridView1.HorizontalScrollingOffset;
        //            textArea.Y += rowBounds.Height - e.InheritedRowStyle.Padding.Bottom;
        //            textArea.Height -= rowBounds.Height -
        //                e.InheritedRowStyle.Padding.Bottom;
        //            textArea.Height = (textArea.Height / e.InheritedRowStyle.Font.Height) *
        //                e.InheritedRowStyle.Font.Height;

        //            // Calculate the portion of the text area that needs painting.
        //            RectangleF clip = textArea;
        //            clip.Width -= this.dataGridView1.RowHeadersWidth + 1 - clip.X;
        //            clip.X = this.dataGridView1.RowHeadersWidth + 1;
        //            RectangleF oldClip = e.Graphics.ClipBounds;
        //            e.Graphics.SetClip(clip);

        //            // Draw the content that spans multiple columns.
        //            e.Graphics.DrawString(
        //                text, e.InheritedRowStyle.Font, forebrush, textArea);

        //            e.Graphics.SetClip(oldClip);
        //        }
        //    }
        //    finally
        //    {
        //        forebrush.Dispose();
        //    }

        //    if (this.dataGridView1.CurrentCellAddress.Y == e.RowIndex)
        //    {
        //        // Paint the focus rectangle.
        //        e.DrawFocus(rowBounds, true);
        //    }
        //}
        #endregion

        private void vistaButton6_Click(object sender, EventArgs e)
        {

        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void button5_MouseDown(object sender, MouseEventArgs e)
        {

           
            button5.Image = imageList1.Images[1];
        }

        private void button5_MouseHover(object sender, EventArgs e)
        {

        }

        private void button5_MouseUp(object sender, MouseEventArgs e)
        {
            button5.Image = imageList1.Images[0];
        }

        private void button5_KeyDown(object sender, KeyEventArgs e)
        {

        }

        private void button5_Click(object sender, EventArgs e)
        {

        }

        private void button1_MouseDown(object sender, MouseEventArgs e)
        {
            button1.Image = imageList1.Images[3];
        }

        private void button1_MouseUp(object sender, MouseEventArgs e)
        {
            button1.Image = imageList1.Images[2];
        }

        private void button2_MouseDown(object sender, MouseEventArgs e)
        {
            button2.Image = imageList1.Images[5];
        }

        private void button2_MouseUp(object sender, MouseEventArgs e)
        {
            button2.Image = imageList1.Images[4];
        }

        private void button3_MouseDown(object sender, MouseEventArgs e)
        {
            button3.Image = imageList1.Images[7];
        }

        private void button3_MouseUp(object sender, MouseEventArgs e)
        {
            button3.Image = imageList1.Images[6];
        }

        private void button4_MouseDown(object sender, MouseEventArgs e)
        {
            button4.Image = imageList1.Images[9];
        }

        private void button4_MouseUp(object sender, MouseEventArgs e)
        {
            button4.Image = imageList1.Images[8];
        }

        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
           // Graphics gfx = e.Graphics;
           // Rectangle rect = e.ClipRectangle;
           //// gfx.Clear(Color.Transparent);
           // //gfx.FillRectangle(new SolidBrush(Color.Blue), rect);

           // Image background = Image.FromFile(@"E:\项目\ProjectFramework\服装单店进销存\Addin1\backcolor2.JPG");
           // gfx.DrawImage(background,0,0,1400,1000);
        }

        private void Buttons_Enter(object sender, EventArgs e)
        {

        }

    }
}
